Repository navigation
feat(auth): support universeDomain in OAuth2CredentialsWithRefresh - #15180
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for custom universe domains in OAuth2CredentialsWithRefresh by adding a universeDomain field, corresponding builder methods, and updating the equals and hashCode implementations. It also adds comprehensive unit tests for these changes. The feedback suggests a critical improvement to the equals and hashCode methods: excluding the refreshHandler functional interface from these checks, as comparing lambdas for equality is fragile. Additionally, the feedback recommends using the getUniverseDomain() getter instead of the direct field access to ensure consistency, along with corresponding updates to the unit tests.
| @Test | ||
| void equals_true() { | ||
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN; | ||
| OAuth2CredentialsWithRefresh credentials = | ||
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | ||
| OAuth2CredentialsWithRefresh other = | ||
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | ||
|
|
||
| assertEquals(credentials, credentials); | ||
| assertEquals(credentials, other); | ||
| assertEquals(other, credentials); | ||
| assertEquals(credentials.hashCode(), other.hashCode()); | ||
| } |
There was a problem hiding this comment.
Update the test to use two different lambda instances for the refresh handler. This demonstrates that different handler instances do not affect equality when the tokens and domains are identical.
| @Test | |
| void equals_true() { | |
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN; | |
| OAuth2CredentialsWithRefresh credentials = | |
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | |
| OAuth2CredentialsWithRefresh other = | |
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | |
| assertEquals(credentials, credentials); | |
| assertEquals(credentials, other); | |
| assertEquals(other, credentials); | |
| assertEquals(credentials.hashCode(), other.hashCode()); | |
| } | |
| @Test | |
| void equals_true() { | |
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler1 = () -> ACCESS_TOKEN; | |
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler2 = () -> ACCESS_TOKEN; | |
| OAuth2CredentialsWithRefresh credentials = | |
| createTestBuilder(handler1).setUniverseDomain("example.com").build(); | |
| OAuth2CredentialsWithRefresh other = | |
| createTestBuilder(handler2).setUniverseDomain("example.com").build(); | |
| assertEquals(credentials, credentials); | |
| assertEquals(credentials, other); | |
| assertEquals(other, credentials); | |
| assertEquals(credentials.hashCode(), other.hashCode()); | |
| } |
| void equals_false() { | ||
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN; | ||
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler otherHandler = () -> ACCESS_TOKEN; | ||
| AccessToken otherToken = | ||
| new AccessToken("otherAccessToken", new Date(ACCESS_TOKEN.getExpirationTime().getTime())); | ||
| OAuth2CredentialsWithRefresh baseline = | ||
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | ||
|
|
||
| List<Consumer<OAuth2CredentialsWithRefresh.Builder>> mutations = | ||
| Arrays.asList( | ||
| b -> b.setAccessToken(otherToken), | ||
| b -> b.setRefreshHandler(otherHandler), | ||
| b -> b.setUniverseDomain("other.example.com")); | ||
|
|
||
| assertNotEquals(baseline, null); | ||
| assertNotEquals(baseline, "not-a-credential"); | ||
| for (Consumer<OAuth2CredentialsWithRefresh.Builder> mutation : mutations) { | ||
| OAuth2CredentialsWithRefresh.Builder builder = | ||
| createTestBuilder(handler).setUniverseDomain("example.com"); | ||
| mutation.accept(builder); | ||
| OAuth2CredentialsWithRefresh other = builder.build(); | ||
| assertNotEquals(baseline, other); | ||
| assertNotEquals(other, baseline); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Remove the refreshHandler mutation from the list of mutations since the refresh handler is excluded from equals and hashCode comparisons.
@Test
void equals_false() {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN;
AccessToken otherToken =
new AccessToken("otherAccessToken", new Date(ACCESS_TOKEN.getExpirationTime().getTime()));
OAuth2CredentialsWithRefresh baseline =
createTestBuilder(handler).setUniverseDomain("example.com").build();
List<Consumer<OAuth2CredentialsWithRefresh.Builder>> mutations =
Arrays.asList(
b -> b.setAccessToken(otherToken),
b -> b.setUniverseDomain("other.example.com"));
assertNotEquals(baseline, null);
assertNotEquals(baseline, "not-a-credential");
for (Consumer<OAuth2CredentialsWithRefresh.Builder> mutation : mutations) {
OAuth2CredentialsWithRefresh.Builder builder =
createTestBuilder(handler).setUniverseDomain("example.com");
mutation.accept(builder);
OAuth2CredentialsWithRefresh other = builder.build();
assertNotEquals(baseline, other);
assertNotEquals(other, baseline);
}
}…doc and simplify tests
…oogleapis#15180) Adds `universeDomain` support to `OAuth2CredentialsWithRefresh`: - Adds `universeDomain` field (defaulting null or blank values to `googleapis.com`), overrides `getUniverseDomain()`, adds `Builder.setUniverseDomain()` and `Builder.getUniverseDomain()`, and includes `universeDomain` in `equals()` and `hashCode()`. - Pins `serialVersionUID` to the prior computed value and normalizes null or blank `universeDomain` in `readObject()` for deserialization compatibility with older streams. - Updates `OAuth2Credentials.create(AccessToken)` and `OAuth2Credentials.newBuilder()` Javadocs to note the `googleapis.com` default and point custom universe domain callers to `GoogleCredentials`. - Adds unit tests in `OAuth2CredentialsWithRefreshTest` covering builder and resolved universe domains, protected constructor default, serialization round-trips and legacy null/blank deserialization, and `equals()`/`hashCode()` (including default normalization and null initial access tokens).
Adds
universeDomainsupport toOAuth2CredentialsWithRefresh:universeDomainfield (defaulting null or blank values togoogleapis.com), overridesgetUniverseDomain(), addsBuilder.setUniverseDomain()andBuilder.getUniverseDomain(), and includesuniverseDomaininequals()andhashCode().serialVersionUIDto the prior computed value and normalizes null or blankuniverseDomaininreadObject()for deserialization compatibility with older streams.OAuth2Credentials.create(AccessToken)andOAuth2Credentials.newBuilder()Javadocs to note thegoogleapis.comdefault and point custom universe domain callers toGoogleCredentials.OAuth2CredentialsWithRefreshTestcovering builder and resolved universe domains, protected constructor default, serialization round-trips and legacy null/blank deserialization, andequals()/hashCode()(including default normalization and null initial access tokens).